package client;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import service.IService;

public class ProfileTest extends AbstractTest implements IService {
	public static void main(String args[]) throws Exception {
        

    	//testRequestJsonGetProfiles();
    	
    	testRequestJsonPutProfile();
    	//testRequestJsonRemoveProfile();
    	//testRequestJsonGetProfile();
    	
        System.out.println("\n"); 
        System.exit(0);
    }

	private static void testRequestJsonGetProfiles()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get single catalog info, expect JSON");
        GetMethod get = new GetMethod(getTestHost()+ACTION_PROFILES+"/1");
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
	private static void testRequestJsonGetProfile()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get single profile info, expect JSON");
        GetMethod get = new GetMethod(getTestHost()+ACTION_PROFILE+"/1");
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
    private static void testRequestJsonPutProfile()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP PUT request to save profile info, use JSON, expect RESPONSE");
        PutMethod post = new PutMethod(getTestHost()+ACTION_SAVEPROFILE+"/0");
        post.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
        	
        	
        	post.setRequestEntity(new StringRequestEntity(
        			" {\"Profile\":[{\"id\":0,\"name\":\"DEMO1\",\"type\":1,\"appset\":\"testappset\",\"application\":\"testapplication\",\"username\":\"testuser\",\"password\":\"testpasswort\"}]}"));

            int result = httpclient.executeMethod(post);
            
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            post.releaseConnection();            
        }
    }

    private static void testRequestJsonRemoveProfile()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP POST request to remove profile, use JSON, expect RESPONSE");
        PostMethod post = new PostMethod(getTestHost()+ACTION_REMOVEPROFILE+"/1");
        post.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
        	
            int result = httpclient.executeMethod(post);
            
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            post.releaseConnection();            
        }
    }
}
